import { WeaTools } from 'ecCom';
const intFunction = (func) => {
    try {
        const myScript = document.createElement('script');
        myScript.appendChild(document.createTextNode(func));
        document.body.appendChild(myScript);
    } catch (e) {
        if (!!(window.console && window.console.log)) {
            window.console.log(e);
        }
    }
}

export default (formulaObj) => {
    const { name } = formulaObj;
    const content = WeaTools.Base64.decode(formulaObj.content);
    intFunction(content);
    class TempFunction extends GC.Spread.CalcEngine.Functions.Function {
        constructor(name, minArgs, maxArgs, description) {
            super(name, minArgs, maxArgs, description);
            this.typeName = `customFunction_${name}`;
        }
        evaluate = (...args) => {
            const func = eval(name);
            if (typeof func === 'function') {
                return func(...args);
            }
            return 0;
        }
        acceptsReference = () => true
    }
    window[`customFunction_${name}`] = TempFunction;
    return TempFunction;
}